package com.buaa.daas.controller;


import com.buaa.daas.entity.StuFeedback;
import com.buaa.daas.entity.StuInfo;
import com.buaa.daas.service.StuFeedbackService;
import com.buaa.daas.service.StuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangKangMing
 * @since 2021-06-04
 */
@Controller
public class StuFeedbackController {

    @Autowired
    private StuFeedbackService stuFeedbackService;

    @Autowired
    private StuInfoService stuInfoService;

    @GetMapping("/feedback")
    String stuFeedback(Model model){
        List<StuFeedback> stuFeedbacks =  stuFeedbackService.findAll();
        model.addAttribute("stuFeedbacks",stuFeedbacks);
        return "feedback";
    }

    @PostMapping("/feedback")
    String postStuFeedcack(@RequestParam("feedbackText") String feedbackText, HttpServletRequest request, Model model){

        StuFeedback stuFeedback = new StuFeedback();
        stuFeedback.setStuFeeback(feedbackText);

        StuInfo stuInfo = stuInfoService.getInfoByRequest(request);
        String stuNum = stuInfo.getStuNum();
        stuFeedback.setStuNum(stuNum);

        stuFeedbackService.save(stuFeedback);
        List<StuFeedback> stuFeedbacks =  stuFeedbackService.findAll();
        model.addAttribute("stuFeedbacks",stuFeedbacks);

        return "feedback";
    }

}

